#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW 0
#define OK 1
#define ERROR 0
typedef int SElemType;
typedef int Status;
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
} SqStack;

Status InitStack(SqStack &S)
{
    S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if (!S.base)
        exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}

Status Push(SqStack &S, SElemType e)
{
    if (S.top - S.base >= S.stacksize)
    {
        S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
        if (!S.base)
            exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top = e;
    S.top++;
    return OK;
}

Status Pop(SqStack &S, SElemType &e)
{
    if (S.top == S.base)
        return ERROR;
    S.top--;
    e = *S.top;
    return OK;
}

Status StackEmpty(SqStack S)
{
    if (S.top == S.base)
        return 1;
    else
        return 0;
}

int main(int argc, char const *argv[])
{
    while (true)
    {
        SqStack S;
        InitStack(S);
        int N, e, m;
        printf("请输入要转换的数值：");
        scanf("%d", &N);
        printf("请输入要转换的数制：");
        scanf("%d", &m);
        while (N)
        {
            Push(S, N % m);
            N = N / m;
        }
        printf("转换后的结果是：\n");
        while (!StackEmpty(S))
        {
            Pop(S, e);
            printf("%d", e);
        }
        printf("\n");
    }
    return 0;
}
